import Request from '@/api/request.js'

/**
 * 登录凭证校验，获取登录信息
 * @param code 临时登录凭证
 */
export function login (code) {
	return Request.request({
		isToken: false,
		url: 'wxa/login',
		method: 'GET',
		data: { code }
	});

 //  return request.ajax({
 //    url: 'wxa/login',
 //    params: { code },
 //    method: Method.GET,
	// needToken: false
 //  })
}

/**
 * 统一下单
 * @param order 订单信息 
 */
export function unifiedorder (order) {
	return Request.request({
		isToken: true,
		url: 'wxa/unifiedorder',
		method: 'GET',
		data: order
	});
	
 //  return request.ajax({
 //    url: 'wxa/unifiedorder',
 //    data: order,
	// isJson: true,
 //    method: Method.POST,
	// needToken: false
 //  })
}

/**
 * 支付
 * @param order 统一订单信息 
 */
export function pay (order, success, fail) {
  uni.requestPayment({
    provider: 'wxpay',
  	timeStamp: order.timeStamp,
  	nonceStr: order.nonceStr,
  	package: order.packageValue,
  	signType: 'MD5',
  	paySign: order.paySign,
  	success: function (payRes) {
  		console.log('success:' + JSON.stringify(payRes));
  		uni.showToast({
  			title: '支付成功'
  		})
  		if(success)success(payRes);
  	},
  	fail: function (err) {
		if(err.errMsg == 'requestPayment:fail cancel'){
			uni.showToast({
				title: '支付已取消',
				icon: 'none'
			})
		} else{
			uni.showToast({
				title: err.errMsg,
				icon: 'none'
			})
			if(fail)fail(err);
		}
  	}
  });
}
